package org.dfl.core.data.types;

/**
 * Type container used in type map to define its value type.
 * Attributes: 
 * parentMap - a map type that holds this type container
 * key - key in the map
 * removeKeyWhenAny - if true and type in this type container changes to AnyType then remove the key in a map

 * @author Piotr Wlodarczyk
 *
 */public class SubMapTypeContainer extends TypeContainer {
	private String key;
	private boolean removeKeyWhenAny;
	private MapType parentMap;

	/**
	 * create a type container assigned to a specific key in a map type
	 * @param key	- key in a map
	 * @param type	- initial type for a type container (value in a map for a specific type)
	 * @param parentMap - assigned map
	 * @param removeKeyWhenAny	- removable when AnyType
	 */
	public SubMapTypeContainer(String key, Type type, MapType parentMap, boolean removeKeyWhenAny) {
		super(type);
		this.key = key;
		this.parentMap = parentMap;
		this.removeKeyWhenAny = removeKeyWhenAny;
	}
	
	@Override
	public void setType(Type type) {
		super.setType(type);
		if(type instanceof AnyType && removeKeyWhenAny){
			parentMap.removeSubkey(key);
		}
	}

	public SubMapTypeContainer copy() {
		return new SubMapTypeContainer(key,getType().getCopy(),parentMap,removeKeyWhenAny);
	}

	public boolean complain(SubMapTypeContainer container) {
		return getType().complain(container.getType());
	}

	public boolean isRemoveKeyWhenAny() {
		return removeKeyWhenAny;
	}

	public void setRemoveKeyWhenAny(boolean removeKeyWhenAny) {
		this.removeKeyWhenAny = removeKeyWhenAny;
	}
	
	
}
